package megrez.mail.sender;

import java.util.Locale;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;
import megrez.mail.sender.domain.TemplateContent;
import megrez.mail.sender.domain.TemplateContentRepository;
import megrez.mail.sender.domain.MailContentType;
import megrez.mail.sender.domain.TemplateConfig;
import megrez.mail.sender.domain.TemplateConfigRepository;

/**
 * 启动定义的任务
 * 
 * @author Lucky Yang
 * @since 2.6.9
 */
@Component
@Slf4j
public class CreateTemplateRunner implements CommandLineRunner {
    public static final String TEXT_TEMPLATE = "text";
    public static final String THYMELEAF_TEMPLATE = "thymeleaf";
    public static final String FREEMARKER_TEMPLATE = "freemarker";
    public static final String GROOVY_TEMPLATE = "groovy";
    public static final String PUG4J_TEMPLATE = "pug4j";

    @Autowired
    private TemplateConfigRepository mailTemplateRep;
    @Autowired
    private TemplateContentRepository mailContentRepo;

    @Override
    public void run(String... args) throws Exception {
        createTextTemplate(TEXT_TEMPLATE);
        createThymeleafTemplate(THYMELEAF_TEMPLATE);
        createFreemarkerTemplate(FREEMARKER_TEMPLATE);
        createGroovyTemplate(GROOVY_TEMPLATE);
        createPug4jTemplate(PUG4J_TEMPLATE);
    }

    private void createTextTemplate(String code) {
        if (mailTemplateRep.existsByCode(code)) {
            return;
        }

        log.info("Creating text template [{}]", code);
        TemplateConfig template = new TemplateConfig(MailContentType.TEXT, code, "文本模板测试", true);
        mailTemplateRep.save(template);

        TemplateContent content = new TemplateContent("文本测试邮件", Locale.getDefault(), "测试邮件，邮件发送时间[${sendTime}]", template);
        mailContentRepo.save(content);
    }

    private void createThymeleafTemplate(String code) {
        if (mailTemplateRep.existsByCode(code)) {
            return;
        }

        log.info("Creating thymeleaf template [{}]", code);
        TemplateConfig template = new TemplateConfig(MailContentType.THYMELEAF, code, "Thymeleaf模板测试", true);
        mailTemplateRep.save(template);

        
        TemplateContent content = new TemplateContent("Thymeleaf模板测试", Locale.getDefault(), "<h1>测试邮件，邮件发送时间<span th:text=\"${sendTime}\"></span></h1>", template);
        mailContentRepo.save(content);
    }

    private void createFreemarkerTemplate(String code) {
        if (mailTemplateRep.existsByCode(code)) {
            return;
        }

        log.info("Creating freemarker template [{}]", code);
        TemplateConfig template = new TemplateConfig(MailContentType.FREEMARKER, code, "Freemarker模板测试", true);
        mailTemplateRep.save(template);

        
        TemplateContent content = new TemplateContent("Freemarker模板测试", Locale.getDefault(), "<h1>测试邮件，邮件发送时间<span>${sendTime}</span></h1>", template);
        mailContentRepo.save(content);
    }

    private void createGroovyTemplate(String code) {
        if (mailTemplateRep.existsByCode(code)) {
            return;
        }

        log.info("Creating groovy template [{}]", code);
        TemplateConfig template = new TemplateConfig(MailContentType.GROOVY, code, "Groovy模板测试", true);
        mailTemplateRep.save(template);

        
        TemplateContent content = new TemplateContent("Groovy模板测试", Locale.getDefault(), "h1 {p('测试邮件，邮件发送时间') span(sendTime)}", template);
        mailContentRepo.save(content);
    }

    private void createPug4jTemplate(String code) {
        if (mailTemplateRep.existsByCode(code)) {
            return;
        }

        log.info("Creating pug4j template [{}]", code);
        TemplateConfig template = new TemplateConfig(MailContentType.PUG4J, code, "Pug4j模板测试", true);
        mailTemplateRep.save(template);

        
        TemplateContent content = new TemplateContent("Pug4j模板测试", Locale.getDefault(), "h1 测试邮件，邮件发送时间 <span> #{sendTime}</span> ", template);
        mailContentRepo.save(content);
    }
}
